<!-- 编辑弹窗 -->
<template>
    <ele-modal
      width="50%"
      inner
      :visible="visible"
      :close-on-click-modal="true"
      custom-class="ele-dialog-form"
      :title="isUpdate ? '客户提成核销' : '客户提成核销'"
      @update:visible="updateVisible" 
    >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
        <el-card  >        
          <el-row :gutter="24">
            <el-col :md="12" :sm="24">
              <el-form-item label="单据编号:" prop="orderNo">
                <el-input v-model="form.orderNo" placeholder="请选择单据编号"  disabled >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="付款账户:" prop="bankAccountId">
                <el-select v-model="form.bankAccountId" clearable placeholder="请选择付款账户" style="width:100%" >
                  <el-option v-for="(item, index) in bankAccountDatas" :key="index" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24" >
              <el-form-item label="付款日期:" prop="recDate">
                <el-date-picker v-model="form.recDate" type="date" placeholder="请输入付款日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width:100%"/>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="币种:" >
                <el-select v-model="form.currencyId" clearable placeholder="请选择币种" style="width:100%" disabled >
                  <el-option v-for="(item, index) in currencyDatas" :key="index" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="汇率:" >
                <el-input v-model="form.rate"  disabled  >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="付款金额:" prop="amount">
                <el-input v-model="form.amount" placeholder="请输入付款金额"  @change="changeAmount"  >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="付款金额" label-width="100px" prop="amountCn" >
                <el-input v-model="form.amountCn"  disabled  prefix-icon="el-icon-_rmb" >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="备注:">
                <el-input v-model="form.remark" placeholder="请输入备注"  :rows="4" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </div>
    </ele-modal>
  </template>
  <script>

  //初始化对像
  import { getUser } from '@/utils/user-util';
  import { isIntegerOrDecimal2} from '@/utils/validate';
  import { GetAllAccount } from '@/api/usermanager/accountmanager';
  import { GetAllBackAccount } from '@/api/basicdatamanager/bankaccount';
  import { QueryAllChildDictViewByCode } from '@/api/systemmanager/dictmanager';
  import { AddPaycustomercommissionitem } from '@/api/finance/recreceipt';
  import { QueryCustomerPageList } from '@/api/basicdatamanager/customer';

  export default {
    name: 'SalecommissionItemEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    data() {
      return {
        // 表单数据
        form: {},
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        operatorDatas:[],
        currencyDatas:[],
        customerDatas:[],
        bankAccountDatas:[],
        defaultDate:new Date(),
        // 表单验证规则
        rules: {
          amount: [{required: true,message: '请输入付款金额',trigger: 'blur'}],
          recDate: [{required: true,message: '请输入付款日期',trigger: 'blur'}],
          bankAccountId: [{required: true,message: '请输入付款账户',trigger: 'blur'}],
        },
      };
    },
    created(){
      this.initData();
    },
    methods: {

      /* 初始化数量 */
     async initData(){

        GetAllAccount().then((data)=>{
          this.operatorDatas = [];
          this.operatorDatas = data;
        });

        GetAllBackAccount().then((data)=>{
          this.bankAccountDatas = [];
          this.bankAccountDatas = data;
        });

        QueryAllChildDictViewByCode(this.Constant.C_HB).then((data)=>{
          this.currencyDatas = [];
          this.currencyDatas = data;
        });

        let params = {
          pageindex: 1,
          pagesize: 100000,
          conditions: [],
          orderBys: [{ sort: "createDate", order: 1, }],
        };

        let result = await QueryCustomerPageList(params);
        this.customerDatas=[];
        this.customerDatas = result.items;

      },

      /* 保存编辑 */
      save() {
        //校验必填信息
        this.$refs['form'].validate((valid) => {
          if (!valid) {return false;}

          let pamount = parseFloat(this.form.payAmount);
          let samount = parseFloat(this.data.surplusAmount);
          if(pamount>samount){
            this.$message.info("付款金额不能大于未付金额");
            this.form.payAmount = this.data.surplusAmount;
            return
          }

          this.loading = true;
          AddPaycustomercommissionitem(this.form)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              //回调父组件方法
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },

      changeAmount(){
        if(this.form.payAmount && this.form.rate){
          let rate = parseFloat(this.form.rate);
          let pamount = parseFloat(this.form.payAmount);
          let samount = parseFloat(this.data.surplusAmount);

          if(pamount>samount){
            this.$message.info("付款金额不能大于未付金额");
            this.form.payAmount = this.data.surplusAmount;
            return
          }else{
            this.form.payAmountCn = (pamount/rate).toFixed(4);
          }
        }
      },

      changeSale(val){
       let operator =  this.operatorDatas.find((d)=>{
          return d.id == val;
        });
        if(operator){
          this.form.payUserName = operator.name;
        }
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
        //更新文件列表对像
      },
  },
    
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          this.form = this.data;
          this.form.id = '';
          this.isUpdate = false;
          this.form.payAmount = this.data.surplusAmount;

          if(this.data.rate && this.data.surplusAmount){
            let r = parseFloat(this.data.rate);
            let s = parseFloat(this.data.surplusAmount);
            this.form.payAmountCn = (s/r).toFixed(4);
          }

          let currentUser = getUser();
          this.$set(this.form,'payDate',this.defaultDate);
          this.$set(this.form,'payUser',currentUser.accountId);
          this.$set(this.form,'payUserName',currentUser.accountName);
        }
      }
    }
  };
  </script>